

from django.contrib import admin
from django.utils.safestring import mark_safe

from mars.utils.common import ExportExcelMixin

from .models import Order

# Register your models here.


@admin.register(Order)
class OrdergAdmin(admin.ModelAdmin,ExportExcelMixin):
    """订单统计"""

    # 需要显示的字段信息
    list_display = ("order_no", "func_name", "price", "num","total","nickname","status_state","create_time")

    # 详情页显示字段
    fields  = ("order_no", "func_name", "price", "num","total","nickname","create_time")
    actions = ['export_as_excel']


    def status_state(self,obj):
        if obj.status == 0:
            status = '未支付'
            color_code = 'red'
        else:
            color_code = 'green'
            status = '已支付'
        return mark_safe(
            '<span style="color:{0};">{1}</span>'.format(color_code,status)

        )
    status_state.short_description = '支付状态'

    # 设置显示小程序用户的openid
    def nickname(self, obj):
        return obj.user.oauthmpuser_set.first().nickname
    nickname.short_description = "昵称"

    # 禁止新增、修改和删除功能
    def has_add_permission(self, request):
        """禁用增加按钮"""
        return False
    
    def  has_delete_permission(self, request, obj=None):
        """禁用删除按钮"""
        return False

    def has_change_permission(self, request, obj=None):
        return False



        
    